package com.example.demo.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.LambdaMeta;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.domain.po.BookEntity;
import com.example.demo.domain.po.BookTypeEntity;
import com.example.demo.service.BookService;
import com.example.demo.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/book")
public class BookController {
    @Autowired
    private BookService bookService;
    @PostMapping("/getAllBook")
    public R getBooks() {

        List<BookEntity> entitys = bookService.getBooks();

        return R.success(entitys);
    }

    @PostMapping("/getNotBorrowedBooks")
    public R getNotBorrowedBooks (){
        List<BookEntity> entities = bookService.getNotBorrowedBooks();
        return R.success(entities);
    }
    @PostMapping("/deleteBook")
    public R deleteBook(@RequestBody BookEntity bookEntity) {
        int x = bookService.deleteBook(bookEntity);
        String 	str = "delete success";
        return R.success(str);
    }
    @PostMapping("/addBook")
    public R addBook(@RequestBody BookEntity bookEntity){
        BookEntity entity = bookService.addBook(bookEntity);
        return R.success(entity);
    }
    @PostMapping("/updateBook")
    public R updateBook(@RequestBody BookEntity bookEntity){
        boolean status = bookService.updateBook(bookEntity);
        return R.success(status);
    }
    @PostMapping("/getBooksByAuthor")
    public R getBooksByAuthor(@RequestBody JSONObject params){
          String author = params.getString("author");
          List<BookEntity> entitys = bookService.getBooksByAuthor(author);
          return R.success(entitys);

    }
    @PostMapping("/getBooksByBookType")
    public R getBooksByBookType(@RequestBody JSONObject params){
        String bookType = params.getString("bookType");
        List<BookEntity> entitys = bookService.getBooksByBookType(bookType);
        return R.success(entitys);
    }
    @PostMapping("/getBooksByUserName")
    public R getBooksByUserName(@RequestBody JSONObject params){
        String username = params.getString("username");
        List<BookEntity> entitys = bookService.getBooksByUserName(username);
        return R.success(entitys);
    }
}